#pragma once

#include <optixu/optixu_math_namespace.h>


__device__ __inline__ void sample_envmap( const optix::float2 &sample, const optix::float3 ffnormal, optix::float3 &L, float &pdf)
{
	using namespace optix;
	const float z4=sample.x;
	const float z5=sample.y;
	const float z6 = powf(fold(z4), 2.0f / (1.0f + 1.0f));
	const float sintheta = sqrtf(1.0f - z6);
	const float costheta = sqrtf(z6);
	const float phi = (2.0f * M_PIf) * z5;
	const float cosphi = cosf(phi) * sintheta;
	const float sinphi = sinf(phi) * sintheta;
	float3 v3,v4;
	create_onb(-ffnormal, v3, v4);
	L = (-ffnormal)*costheta + v3 * cosphi + v4 * sinphi;
	pdf = dot( ffnormal, L );
}




